package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Goods;
import com.hl.card.common.util.HttpUtils;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.shiro.ShiroKit;
import com.hl.card.modular.pack.service.ManualRewardByTrans;
import com.hl.card.modular.packBack.service.GoodsSevice;
import com.hl.card.modular.system.service.IAccessRight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 参数设置控制器
 *
 * @author huangpu
 * @Date 2018-04-06 11:33:32
 */
@Controller
@RequestMapping("/wx")
public class WxAccessDemoController extends BaseController {
    @Autowired
    private ManualRewardByTrans rewardByTrans;

    @Resource
    private IAccessRight accessRight;

    @Resource
    private GoodsSevice goodsSevice;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private String PREFIX = "/packback/manualAward/";

    /**
     * 跳转到微信授权演示首页
     */
    @RequestMapping("/index.html")
    public String index(HttpServletRequest request, ModelMap model) {
        return checkAccess(request, model);
    }


    private String checkAccess(HttpServletRequest request, ModelMap model) {
        String clientIP = HttpUtils.getIpAddr(request);
//        List<Map<String, Object>> list= goodsSevice.selectList();
//        for(Object obj:list) {
//            Goods goods = (Goods) obj;
//            System.out.println("good:" + goods.getSeries() + goods.getNowtype());
//        }
        stringRedisTemplate.opsForValue().set(clientIP + "redirectUrl", "/wx/index.html");
        List<Map<String, Object>> list= goodsSevice.selectList();
        for(Object obj:list) {
            Goods goods = (Goods) obj;
            System.out.println("good:" + goods.getSeries() + goods.getNowtype());
        }
        model.put("notify", "微信授权测试");
        model.addAttribute("goods",list);
        LogObjectHolder.me().set(list);
        model.put("successUrl", "/wx/index.html");
        return  accessRight.baseCheckWxAccess(request, "WX_OPT_DEMO", PREFIX + "manual_award_add.html", model);
    }

    @PostMapping(value = "/byUserId")
    @ResponseBody
    public ResponseObject userReward(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String parentCode = request.getParameter("parentCode");
        String prodCode = request.getParameter("prodCode");
        String amount = request.getParameter("amount");
        String count = request.getParameter("count");
        String accessCode = request.getParameter("accessCode");
        String clientIP = HttpUtils.getIpAddr(request);
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非法操作,请重新获取授权");
        //获取当前操作账户
        String account = ShiroKit.getUser().getAccount();
        if(accessRight.checkWxSysOptAccessRight("WX_OPT_DEMO", clientIP)) {
            return rewardByTrans.userReward(userId, parentCode, prodCode, amount, count, accessCode, clientIP, account);
        }else {
            return reObj;
        }
    }

    @PostMapping(value = "/byPhone")
    @ResponseBody
    public ResponseObject rewardByPhone(HttpServletRequest request) {
        String mobile = request.getParameter("mobile");
        String parentCode = request.getParameter("parentCode");
        String prodCode = request.getParameter("prodCode");
        String amount = request.getParameter("amount");
        String count = request.getParameter("count");
        String accessCode = request.getParameter("accessCode");
        String clientIP = HttpUtils.getIpAddr(request);
        System.out.println("来访IP:" + clientIP);
        System.out.println("mobile:" + mobile);
        System.out.println("parentCode:" + parentCode);
        System.out.println("prodCode:" + prodCode);
        System.out.println("amount:" + amount);
        System.out.println("count:" + count);
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非法操作,请重新获取授权");
        //获取当前操作账户
        String account = ShiroKit.getUser().getAccount();
        if(accessRight.checkWxSysOptAccessRight("WX_OPT_DEMO", clientIP)) {
            return rewardByTrans.rewardByPhone(mobile, parentCode, prodCode, amount, count, accessCode, clientIP, account);
        }else {
            return reObj;
        }
    }
}
